package com.jindu.producer.system.controller;

import com.jindu.kernel.message.SlideVerification;
import com.jindu.kernel.message.SmsSend;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.result.StatusCode;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.kernel.utils.RandomUtil;
import com.jindu.producer.system.config.MessageSend;
import com.jindu.producer.system.jpa.MobileVerificationJpa;
import com.jindu.producer.system.model.SMobileVerificationEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author jarvis
 * @date 2018/2/21
 */
@Api(description = "手机号验证码管理", tags = "UtilsController", basePath = "/mobile")
@RestController
@RequestMapping(value = "/mobile",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MobileVerificationController {

    @Autowired
    MobileVerificationJpa mobileVerificationJpa;

    @Autowired
    MessageSend messageSend;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据验证码，查询验证码是否有效的信息", notes = "返回单个验证码的信息，用于官网首页的邀请码验证")
    @RequestMapping(value = "getByVerification",method = RequestMethod.GET)
    public JsonResult<SMobileVerificationEntity> getByVerification(@RequestParam int verification){
        return JsonResult.getSuccessResult(mobileVerificationJpa.findByVerification(verification));
    }

    /**
     * 根据用户的手机号，发送验证码，并写入验证码的数据库表
     */
    @ApiOperation(value = "根据用户的手机号，发送验证码，并写入验证码的数据库表", notes = "返回是否发送成功的信息，用于注册的手机验证")
    @RequestMapping(value = "sendVerificationCode",method = RequestMethod.POST)
    public JsonResult<HashMap<String,Object>> sendVerificationCode(@RequestParam String mobile,
                                                                   @RequestParam String lang,
                                                                   @RequestParam String sessionId,
                                                                   @RequestParam String sig,
                                                                   @RequestParam String token,
                                                                   @RequestParam String scene) throws Exception{

        HashMap<String,Object> codeMessage=new HashMap<>();
        SlideVerification sv=new SlideVerification();
        if(sv.verificationSlide(sessionId,sig,token,scene)){
            sendMobileCodeByAliyunIAcs(mobile,lang);

            codeMessage.put(StatusCode.CREATED.getCode().toString(),StatusCode.CREATED.getMeaning());
            return JsonResult.getFailureResult(StatusCode.OK,codeMessage);

        }else {
            codeMessage.put(StatusCode.SlideFailure.getCode().toString(),StatusCode.SlideFailure.getMeaning());
            return JsonResult.getResult(StatusCode.INVALID_REQUEST,codeMessage);
        }

    }

    public SMobileVerificationEntity sendMobileCodeByAliyunIAcs(String mobile,String lang) throws Exception{
        Integer smsCode= RandomUtil.getNum(100000,999999);
        SMobileVerificationEntity mobileVerificationEntity=new SMobileVerificationEntity();

        mobileVerificationEntity.setId(IdGenerator.uuid());
        mobileVerificationEntity.setMobile(mobile);
        mobileVerificationEntity.setVerification(smsCode);
        mobileVerificationEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
        mobileVerificationEntity.setType(2);
        mobileVerificationEntity.setStatus(1);

        SMobileVerificationEntity entity=mobileVerificationJpa.saveAndFlush(mobileVerificationEntity);
        if(entity!=null){

            /**
             * 根据语言，进行相关的设定
             */
            String temp;
            switch (lang){
                case "cn":
                    temp="SMS_132396108";
                    break;
                case "en":
                    temp="SMS_132860001";
                    break;
                default:
                    temp="SMS_132860001";
            }
            SmsSend.sendSms(mobile,temp,smsCode.toString());
        }
        return entity;
    }


}
